Ext.define('Databus.controller.main.MainCtrl', {
	extend : 'Ext.app.Controller',

	stores : ['Databus.store.common.ThemeComboStore'],

	models : ['Databus.model.main.MainLeftTreeMenuModel', 'Databus.model.common.CommonComboModel'],

	views : ['Databus.view.main.MainTopView', 'Databus.view.main.MainLeftView', 'Databus.view.main.MainRightView',
			'Databus.view.main.MainCenterView', 'Databus.view.main.MainBottomView', 'Databus.view.main.ThemeView'],

	refs : [{
				ref : 'mainTopView',
				selector : 'mainTopView'
			}, {
				ref : 'mainBottomView',
				selector : 'mainBottomView'
			}, {
				ref : 'mainLeftView',
				selector : 'mainLeftView'
			}, {
				ref : 'mainRightView',
				selector : 'mainRightView'
			}, {
				ref : 'mainCenterView',
				selector : 'mainCenterView'
			}, {
				ref : 'themeView',
				selector : 'themeView'
			}, {
				ref : 'MainLeftTreeMenuModel',
				selector : 'MainLeftTreeMenuModel'
			}],

	init : function() {
		this.control({
			"mainLeftView" : {
				afterrender : this.loadAccordionMenus
			},
			'mainTopView button[action=logout]' : {
				click : this.mainTopBtnLogout
			},
			'mainTopView menuitem[action=skins]' : {
				click : this.changeSkins
			},
			'mainTopView menuitem[action=basicinfo]' : {
				click : function() {
					alert('basic info');
				}
			},
			'themeView button[action=submitskins]' : {
				click : this.submitChangeSkins
			}

		});
	},

	// logout
	mainTopBtnLogout : function() {
		Ext.Msg.confirm('信息确认', '确认要退出登录么？', function(btn) {
			if (btn == 'yes') {
				window.close();
			}
		}, this);
	},

	// 皮肤
	changeSkins : function() {
		Ext.create('Databus.view.main.ThemeView').show();
	},
	submitChangeSkins : function() {
		var skins = Ext.getCmp("skinsCombo").getValue();
		if (skins == null || Ext.util.Format.trim(skins) == '') {
			Ext.Msg.alert('友情提示', '请选择要切换的皮肤');
			return false;
		}
		var date = new Date();
		date.setTime(date.getTime() + 30 * 24 * 3066 * 1000);
		Ext.util.Cookies.set("system-theme", skins, date);
		this.getThemeView().close();
		window.location.reload();
	},

	// 初始化菜单
	loadAccordionMenus : function() {
		var $app = this;
		var accordion = $app.getMainLeftView();
		var data = Ext.databus.constants.menus;
		var treePanelArr = [];
		Ext.each(data, function(item, index) {
			treePanelArr.push($app.getTreePanel(item));
		});
		accordion.add(treePanelArr);
	},
	getTreePanel : function(item) {
		var $app = this;
		var centerTab = $app.getMainCenterView();

		var nodeId = item.id;
		var treeModel = this.getMainLeftTreeMenuModel();

		var store = Ext.create('Ext.data.TreeStore', {
			root : {
				expanded : true,
				children : item.children
			}
		});

		var openMenu = function(record, itemData, index, e, eOpts) {
			var id = itemData.get('id');
			if (!centerTab.child("#" + id)) {
				var text = itemData.get('text');
				var iconCls = itemData.get('iconCls');;
				var itemPanel = null;
				if (Ext.isEmpty(iconCls)) {
					itemPanel = Ext.create(itemData.raw.url, {
						id : id,
						title : text,
						closable : true
					});
				}
				if (!Ext.isEmpty(iconCls)) {
					itemPanel = Ext.create(itemData.raw.url, {
						id : id,
						title : text,
						iconCls : iconCls,
						closable : true
					});
				}
				centerTab.add(itemPanel);
			}
			centerTab.child("#" + id).show();
		};

		var extTreePanel = Ext.create("Ext.tree.Panel", {
			title : item.text,
			iconCls : item.iconCls,
			rootVisible : false,
			store : store,
			listeners : {
				itemclick : openMenu
			}
		});
		return extTreePanel;
	}

});
